// pages/xuecar/index.js
const app = getApp()
var page = 1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
      //  底部导航
      listInfo:[],
      imgUrls: [],
      loading: true,
      teacherlist:[],
      inputShowed: false,
      inputVal: "",
      showModalStatus: false,
      animationData: '',
      isLoginAuth: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        page = 1;
        if (!app.globalData.isLoginAuth) {
          this.showModal();
        }
        this.setData({
          isLoginAuth: app.globalData.isLoginAuth
        })
        this.initBanner();
        this.getTeachers();
    },
    //滚动到底部刷新
    onReachBottom: function () {
      this.getTeachers();
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.setData({
        listInfo: app.globalData.listInfo
      })
      //console.log(app.globalData.userInfo);
    },
    //广告图片初始化
    initBanner: function () {
      var that = this;
      var requrl = app.globalData.hostUrl + 'common/banner';
      var imgbanner = wx.getStorageSync('indexBanner');
      //console.log(imgbanner.length);
        if (!app.expiratTime('indexBanner') || imgbanner.length==0){
            wx.request({
                url: requrl,
                data: { 'location': '101' },
                header: {
                    'grant-appid': app.globalData.appid
                },
                success: function (res) {
                    if (res.statusCode === 200) {
                        //console.log(res);
                        wx.setStorageSync('indexBanner', res.data.result);
                        that.setData({
                            imgUrls: res.data.result
                        })
                    }
                }
            })
        }
        else{
            that.setData({
                imgUrls: imgbanner
            })
        }
    },
    //获取教练列表
    getTeachers:function(){
        var that = this;
        var limit = 10;
        var requrl = app.globalData.hostUrl + 'common/TeacherByrid';
        var keyname = 'teacher_' + page + '_' + limit;//获取第1页的前面3条；
        var teachers = wx.getStorageSync(keyname);
        if (!app.expiratTime(keyname) || teachers.length == 0) {
            wx.request({
                url: requrl,
                data: { 'page': page, 'psize': limit, 'rid': '教练' },
                header: {
                    'grant-appid': app.globalData.appid
                },
                success: function (res) {
                  if (res.data.code == 200) {
                    wx.setStorageSync(keyname, res.data.result);
                    that.setData({
                      teacherlist: that.data.teacherlist.concat(res.data.result)
                    })
                    if (res.data.result.length < limit) {
                      that.setData({
                        loading: false
                      })
                    }
                  }
                }
            })
        }
        else {
            page++;
            that.setData({
                teacherlist: teachers,
                loading: false
            })
        }

    },
    singup: function (e) {
     // console.log(e);
      var tid = e.currentTarget.dataset.tid;
      var formId = e.detail.formId;
      var openid = wx.getStorageSync('openId');
      var requrl = app.globalData.hostUrl + 'common/getFormid';
      app.saveformid(formId, openid, 'xc', requrl);
      wx.navigateTo({
        url: '/pages/xuecar/signup?tid=' + tid,
      })
    },
    showInput: function () {
      this.setData({
        inputShowed: true
      });
    },
    srckeyword: function () {
        var keyword=this.data.inputVal;
        this.setData({
            inputVal: "",
            inputShowed: false
        });
        wx.navigateTo({
            url: '../search/index?keyword='+keyword,
        })  
    },
    clearInput: function () {
      this.setData({
        inputVal: ""
      });
    },
    inputTyping: function (e) {
      this.setData({
        inputVal: e.detail.value
      });
    },
    gohome: function () {
      wx.redirectTo({
        url: '../index/index',
      })
    },
    yuy: function () {
        if(1==wx.getStorageSync('roleidentity')){
          wx.redirectTo({
            url: '../myself/thappointment',
          })
        }
        else{
          wx.redirectTo({
            url: '../myself/myappointment',
          })
        }
      },
    orders: function () {
        if (1 == wx.getStorageSync('roleidentity')) {
          wx.redirectTo({
            url: '../myself/trorder',
          })
        }
        else {
          wx.redirectTo({
            url: '../myself/myorder',
          })
        }
      },
    mys: function () {
      wx.redirectTo({
          url: '../myself/myself',
        })
      },
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease-in-out",
      delay: 0
    })
    this.animation = animation
    animation.translateY(500).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    this.setData({
      showModalStatus: false,
    })
  },
  bindGetUserInfo: function (e) {
    console.log(e);
    if (e.detail.errMsg == 'getUserInfo:ok') {
      app.globalData.userInfo = e.detail.userInfo;
      app.getfansinfo();
    }
    else if (e.detail.errMsg == "getUserInfo:fail auth deny") {

    }
    else {
      wx.redirectTo({
        url: '../xuecar/index',
      })
    }
  }
})